<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://www.joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		Commercial
*/

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controllerform');

class JSNPluginControllerPlugin extends JControllerForm
{
	/**
	 * @var    string  The prefix to use with controller messages.
	 * @since  1.6
	 */
	protected $text_prefix = 'COM_JSNPLUGIN_PLUGIN';

	/**
	 * Method override to check if you can add a new record.
	 *
	 * @param   array  $data  An array of input data.
	 *
	 * @return  boolean
	 *
	 * @since   1.6
	 */
	
	 
	protected function allowAdd($data = array())
	{
		// Initialise variables.
		$user		= JFactory::getUser();
		if ($allow === null)
		{
			// In the absence of better information, revert to the component permissions.
			return parent::allowAdd($data);
		}
		else
		{
			return $allow;
		}
	}

	
	public function save()
	{
		$db = & JFactory::getDBO();
		$form = $_POST['jform'];
		parent::save();
		if(!$form['id'])
			$pid = $db->insertid();
		else
			$pid = $form['id'];
		$_POST['jform']['language'] = implode(",",$_POST['jform']['language']);
		//store language
		$languages = $_POST['languages'];
		$languages_id = $_POST['data_language_id'];
		$data_language_name = $_POST['data_language_name'];
		$lang_model = $this->getModel('language_data');
		for($k = 0 ; $k<count($languages);$k++)
		{
			$langdata = array();
			$langdata['id'] = $languages_id[$k];
			$langdata['data'] = $languages[$k];
			$langdata['id_plugin'] = $pid;
			$langdata['language'] =$data_language_name[$k];
			$lang_model->save($langdata); 
		}
		//store field event
		$events = $_POST['events'];
		$events_data_id = $_POST['data_event_id'];
		$events_id= $_POST['events_id'];
		$eventdata_model = $this->getModel('event_data');
		for($e=0;$e<count($events);$e++)
		{
			$eventdata= array();
			$eventdata['id'] = $events_data_id[$e];
			$eventdata['data'] = $events[$e];
			$eventdata['id_plugin'] = $pid;
			$eventdata['id_event'] = $events_id[$e];
			$eventdata_model->save($eventdata);
		}
		$_SESSION['reload_language'] = JRequest::getVar('reload_language');
		$_SESSION['reload_event'] = JRequest::getVar('reload_event');

	}	

}
